// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Linq.Expressions;
using Antlr4.Runtime.Tree;

namespace SnapObjects.Expressions
{
    internal class CompileNewExpressionVisitor<TContext> : NewExpressionVisitor<Expression, TContext>
        where TContext : IParseTree
    {
        public CompileNewExpressionVisitor(IServiceProvider services) : base(services)
        {
        }

        public override Expression Visit(
            IExpressionVisitor<Expression> treeVisitor, TContext context, ParserOptions<Expression> options)
        {
            var newOptions = _provider.NestParser.CreateOptions(options);

            var newExpressionVisitor = new NewExpressionVisitor<TContext>(_provider, treeVisitor);

            var expression = newExpressionVisitor.Visit(context, newOptions);

            return _provider.NestParser.Invoke(options, expression, newOptions);
        }
    }
}
